class GrabPanel extends BasePanel {
	public constructor() {
		super();
	}

	public commodityLst: eui.List;
	public timeLst: eui.List;
	public recordLab: eui.Label;
	public bannerImg: ImageComponent;
	public rect: eui.Rect;

	protected childrenCreated() {
		this.timeLst.itemRenderer = GrabTimeItem;
		this.commodityLst.itemRenderer = GrabMatchItem;
		this.timeLst.dataProvider = new eui.ArrayCollection([]);
		this.commodityLst.dataProvider = new eui.ArrayCollection([]);
		this.recordLab.mask = this.rect;
	}

	protected onEnable() {
		this.setCenter();
		this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);
		App.EventManager.addEvent(EventConst.GrabChange, this.changeHandler, this);
	}

	protected onRemove() {
		this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);
		egret.Tween.removeTweens(this.recordLab);
		egret.clearInterval(this.interval);
		egret.clearInterval(this._recordDelay);
		this.interval = null;
		this._recordDelay = null;
	}

	private changeHandler(key: any) {
		if (this.matchList[key]) {
			let info = ProtocolHttp.match_info;
			info = this.matchList[key]["match_info"];
			this.bannerImg.loadImg(info.big_icon_url, "pic_png");	//加载比赛宣传图
			this.commodityLst.dataProvider = new eui.ArrayCollection(info.goods_info);
		}
	}

	private interval;
	/** 更新时间轴 */
	public updateTime() {
		let data = ProtocolHttp.rev_QuicklyGrabTimeAxis.time_infos;
		if (data[0]) {
			let time = ProtocolHttp.grab_time;
			time = data[0];
			if (time.is_start == 0) {
				//当前没有任何一个比赛的话启动一个定时器刷新数据
				if (!this.interval) {
					this.interval = egret.setInterval(() => {
						if (new Date().getTime() >= time.start_time) {
							egret.clearInterval(this.interval);
							this.interval = null;
							App.EventManager.sendEvent(EventConst.UpdateGrab);
						}
					}, this, 2000);
				}
			}
		}
		this.timeLst.dataProvider = new eui.ArrayCollection(data);
	}

	public matchList: Array<Object>;
	public updateMatch() {
		let data = ProtocolHttp.rev_QuicklyGrab.match_data;
		this.matchList = [];
		// 保存所有时间轴的比赛数据
		data.forEach((element) => {
			let match = ProtocolHttp.match_data;
			match = element;
			this.matchList[match.date_hour] = match;
		})
		//取出一个默认比赛数据显示
		if (data[0]) {
			let info = ProtocolHttp.match_info;
			info = data[0]["match_info"];
			this.bannerImg.loadImg(info.big_icon_url, "pic_png");	//加载比赛宣传图
			this.commodityLst.dataProvider = new eui.ArrayCollection(info.goods_info);
		}
	}

	private _recordDelay: number;
	public updateGrabRecord() {
		this.recordLab.y = this.rect.y + this.rect.height - 22;
		let data = ProtocolHttp.rev_QuicklyGrabRecord.grab_record;
		let record = <Array<egret.ITextElement>>[];
		var contentTextStyleJson = { "size": 18, "textColor": 0xFFFFFF, "fontFamily": "黑体" }
		var nameTextStyleJson = { "size": 18, "textColor": 0xFF9800, "fontFamily": "黑体" }
		data.forEach(element => {
			record.push({ text: "恭喜", style: contentTextStyleJson });
			record.push({ text: "[" + element["nickname"] + "]", style: nameTextStyleJson });
			record.push({ text: "获得" + element["good_name"] + " \n", style: contentTextStyleJson });
		});
		this.recordLab.textFlow = record;
		this.recordLab.alpha = 0;

		egret.Tween.get(this.recordLab).to({ alpha: 1 }, 1000).call(this.recordMove, this);
	}

	private recordMove() {
		this._recordDelay = egret.setInterval(() => {
			if (this.recordLab.y > -this.recordLab.height + this.rect.y) {
				egret.Tween.get(this.recordLab).to({ y: this.recordLab.y - 22 }, 200, egret.Ease.bounceOut);
				// this.recordLab.y -= 10;
			} else {
				this.recordLab.y = this.rect.y + this.rect.height - 20;
			}
		}, this, 1000);
	}

	private onTouch(evt: egret.TouchEvent) {
		switch (evt.target) {

			default:
				break;
		}
	}

	private get ctrl(): HallController {
		return App.getController(HallController.NAME) as HallController;
	}
}

class GrabTimeItem extends eui.ItemRenderer {
	constructor() {
		super();
		this.skinName = "GrabItemSkin";
	}

	public timeLab: eui.Label;
	public soonLab: eui.Label;
	public grpNow: eui.Group;
	public countLab: eui.Label;

	protected dataChanged() {

		let time = ProtocolHttp.grab_time;
		time = this.data;
		this.timeLab.text = time.date_string;

		if (time.is_start == 1) {
			//当前正在比赛
			this.count = time.expire_time;
			if (!this.interval) {
				this.countdown();
				this.interval = egret.setInterval(this.countdown, this, 1000);
				this.soonLab.visible = false;
				this.grpNow.visible = true;
			}
		}
	}

	protected childrenCreated() {
		this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);
	}

	private onTouch() {
		App.EventManager.sendEvent(EventConst.GrabChange, this.data["date_hour"]);
	}

	private count: number;
	private interval: number;
	private countdown() {
		this.count--;

		var s = this.count % 60
		var m = Math.floor((this.count / 60)) % 60;
		var h = Math.floor((this.count / 1000 / 60 / 60 % 24));

		if (this.count >= 0) {
			var msg = `距结束 ${(h < 10 ? '0' : '') + h}:${(m < 10 ? '0' : '') + m}:${(s < 10 ? '0' : '') + s}`
			this.countLab.text = msg;
		} else {
			egret.clearInterval(this.interval);
			this.interval = null;

			//刷新快抢赛数据
			App.EventManager.sendEvent(EventConst.UpdateGrab);
		}
	}
}

/** 快抢赛状态 */
enum GrabMatchStatus {
	Disabled,	//禁用
	ToBegin,	//待开始
	Started,	//已开始
}